define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Wanttodistributor, GoAddress, $ionicSlideBoxDelegate, GoDialog, Auth, GoCatTypeAuth,$timeout) {
        //滑动切换
        $scope.selectTab = 0;
        $scope.activeTab = function(index) {
            $scope.selectTab = index;
            $ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
        };
        //滑动切换
        $scope.slideChanged = function(index) {
            $scope.selectTab = index;
        };

        //加载数据
        $scope.init_data = function() {
            Wanttodistributor.getList(1, 10, function(data) {
                $scope.list = data;
                console.log(data)
            })
        }
        $scope.init_data();

        $scope.input = new Object();
        $scope.input.store_id = Auth.get_store_id(); //隐藏的企业id的绑定数据
        $scope.initialize = function(){//初始化数据
        	$scope.input.info_id = "";
	        $scope.input.cat_type_id = ""; //分类
	        $scope.input.region_id = ""; //地区
	        $scope.input.province = null; //省市区三级联动
	        $scope.input.city = null;
	        $scope.input.district = null;
	        $scope.input.cat1 = null;
	        $scope.input.cat2 = null;
	        $scope.input.cat3 = null;
	        $scope.input.phone = "";
	        $scope.input.capital = "";
	        $scope.input.reason = "";
        }
        $scope.initialize();
		
		//监听预投入资金：金额只要2位小数
//		$scope.$watch("input.capital",function(newVal,oldVal){
//		})
		
		
        //下拉侦听，目前只能省市区三级联动
        $scope.listen_province = function(data) {
            $scope.input.city = null;
            $scope.input.district = null;
        };
        $scope.listen_city = function(data) {
            $scope.input.district = null;
        };

        //定义绑定
        var items = new Array();
        var obj1 = {
            name: 'input.province'
        };
        var obj2 = {
            name: 'input.city'
        };
        var obj3 = {
            name: 'input.district'
        };
        items.push(obj1);
        items.push(obj2);
        items.push(obj3);
        GoAddress.init_load($scope, items);
        $scope.doSelectProvince = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择省份",$scope.province_items,$scope.input.province,function(data){
				$scope.input.province = data;
			});
		}
		$scope.doSelectCity = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择城市",$scope.city_items,$scope.input.city,function(data){
				$scope.input.city = data;
			});
		}
		$scope.doSelectDistrict = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县",$scope.district_items,$scope.input.district,function(data){
				$scope.input.district = data;
			});
		}
		$scope.doSelectCat1 = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.cat1_items,$scope.input.cat1,function(data){
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function(){
			//id是值 name是显示
			GoDialog.showSelect("中分类",$scope.cat2_items,$scope.input.cat2,function(data){
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function(){
			//id是值 name是显示
			GoDialog.showSelect("小分类",$scope.cat3_items,$scope.input.cat3,function(data){
				$scope.input.cat3 = data;
			});
		}
        /***分类三级联动***/
        //下拉侦听
        $scope.listen_cat1 = function(data, obj) {
            $scope.input.cat2 = null;
            $scope.input.cat3 = null;
        };
        $scope.listen_cat2 = function(data, obj) {
            $scope.input.cat3 = null;
        };
        //定义绑定
        var items_cat = new Array();
        var obj1_cat = {
            name: 'input.cat1'
        };
        var obj2_cat = {
            name: 'input.cat2'
        };
        var obj3_cat = {
            name: 'input.cat3'
        };
        items_cat.push(obj1_cat);
        items_cat.push(obj2_cat);
        items_cat.push(obj3_cat);
        GoCatTypeAuth.init_load_only($scope, items_cat);

        //发布经销
        $scope.add = function() {
			//分类
        	if($scope.input.cat3){
        		 $scope.input.cat_type_id=$scope.input.cat1.name+"/"+$scope.input.cat2.name+"/"+$scope.input.cat3.name+":"+$scope.input.cat3.id;
        	}else if($scope.input.cat2){
        		 $scope.input.cat_type_id=$scope.input.cat1.name+"/"+$scope.input.cat2.name+":"+$scope.input.cat2.id;
        	}else if($scope.input.cat1){
        		 $scope.input.cat_type_id=$scope.input.cat1.name+":"+$scope.input.cat1.id;
        	}
        	//地域
        	if($scope.input.district){
        		$scope.input.region_id = $scope.input.province.name+"/"+$scope.input.city.name+"/"+$scope.input.district.name+":"+$scope.input.district.id;
        	}else if($scope.input.city){
        		$scope.input.region_id = $scope.input.province.name+"/"+$scope.input.city.name+":"+$scope.input.city.id;
        	}else if($scope.input.province){
        		$scope.input.region_id = $scope.input.province.name+":"+$scope.input.province.id;
        	}

           if($scope.input.cat_type_id=="") {
                GoDialog.toast("请选择品类！");
            } else if($scope.input.region_id=="") {
                GoDialog.toast("请选择代理区域！");
            }else if(!$scope.input.reason) {
                GoDialog.toast("请输入代理描述！");
            }else if(!$scope.input.capital) {
                GoDialog.toast("请输入投入资金！");
            }else{
                Wanttodistributor.beagenter_edit($scope.input.info_id, $scope.input.cat_type_id, $scope.input.region_id, $scope.input.reason, $scope.input.capital, $scope.input.phone, function(data) {
                    if(data.success) {
                        GoDialog.toast("提交成功！");
                       	$scope.activeTab(0); //提交成功返回“已发布的经销”
                        $scope.init_data(); //发布成功，要刷新数据
                        $scope.initialize(); //重置已经发布过的数据
                    } else {
                        GoDialog.toast(data.msg);
                    }
                });
            }
        }

        //根据id查找
        $scope.findById_edit = function(info_id) {
            Wanttodistributor.findById(info_id, function(data) {
                console.log(data);
                $scope.detailById = data;
                $scope.input.info_id = info_id;
             
                $scope.input.phone = $scope.detailById.phone;
                $scope.input.capital = parseFloat($scope.detailById.money).toFixed(2);
                $scope.input.reason = $scope.detailById.requirement;
                $scope.activeTab(1);
                $scope.input.province = null;
                $scope.input.city = null;
                $scope.input.district = null;
                //地区初始化
                GoAddress.init_region($scope, data.region_id, function(val1, val2, val3) {
                    //使用定时器来指引
                    $scope.isUnwatch = true; //不进行侦听操作
                    $scope.input.province = val1;
                    $scope.input.city = val2;
                    $scope.input.district = val3;
                    $timeout(function() {
                        $scope.isUnwatch = false; //不进行侦听操作
                    }, 1000);
                });
                //分类初始化
                GoCatTypeAuth.inti_cat($scope, data.cat_type_id, function(val1, val2, val3) {
                    //使用定时器来指引
                    $scope.isUnwatch = true; //不进行侦听操作
                    $scope.input.cat1 = val1;
                    $scope.input.cat2 = val2;
                    $scope.input.cat3 = val3;
                    $timeout(function() {
                        $scope.isUnwatch = false; //不进行侦听操作
                    }, 1000);
                });


            });
        }

        //删除
        $scope.delete = function(info_id,index) {
            Wanttodistributor.delete_info_id(info_id, function(data) {
                if(data.success) {
                    GoDialog.toast("删除成功！");
                    $scope.list.beagenter.splice(index,1);
                } else {
                    GoDialog.toast(data.msg);
                }
            });
        }


        //下拉刷新
        $scope.input.nPage = 1;
        $scope.isLoad = false; //可以加载
        //下拉刷新 判断哪个模块进行刷新
        $scope.doRefresh = function() {
            //防止重复加载
            $scope.isLoad = true;
            Wanttodistributor.getList(1, 10, function(data)  {
                    console.log(data);
                    if(data != null) {
                        $scope.list = data;
                    }else{
                    	$scope.list = new Object();
                    }
                    $scope.input.nPage = 1;
                },
                function() {
                    $scope.$broadcast('scroll.refreshComplete');
                    $timeout(function() {
                        $scope.isLoad = false;
                    }, 1500);
                });
        };

        //加载更多
        $scope.doRefreshMore = function() {
            //防止重复加载
            $scope.isLoad = true;
            Wanttodistributor.getList($scope.input.nPage +1 ,10, function(data)  {
                    console.log(data);
                    if(data != null) {
                        if($scope.list.beagenter != undefined) {
                            $scope.list.beagenter = $scope.list.beagenter.concat(data.beagenter);
                            $scope.input.nPage += 1;
                        } else {
                            $scope.list = data;
                        }
                    }
                },
                function() {
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                    $timeout(function() {
                        $scope.isLoad = false;
                    }, 1500);

                });

        };

    }

    controller.$inject = ['$scope', 'Wanttodistributor', 'GoAddress', '$ionicSlideBoxDelegate', 'GoDialog', 'Auth', 'GoCatTypeAuth','$timeout'];

    return app.registerController("wanttodistributorctrl",controller);
});